package com.loyal.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.loyal.helper.DialogHelper;
import com.loyal.helper.ErrorHelper;
import com.loyal.helper.UIHelper;
import com.loyal.impl.IFrag2ActImpl;
import com.loyal.impl.UICommandImpl;
import com.loyal.kit.ResUtil;
import com.loyal.ui.activity.BasicActivity;

/**
 * {@link BasicActivity#onFrag2Act(String, Object)}
 */
public abstract class BasicFragment extends Fragment implements UICommandImpl {

    private IFrag2ActImpl mListener;

    public @LayoutRes
    int fragLayoutRes() {
        return 0;
    }

    public abstract void afterOnCreate();

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof IFrag2ActImpl) {
            mListener = (IFrag2ActImpl) context;
        } else {
            throw new RuntimeException(context + " must implement IFrag2ActImpl");
        }
    }

    /**
     * mBinding =  DataBindingUtil.inflate(inflater, fragLayoutRes(),container,false);
     * View view = mBinding.getRoot();
     * initView(view);
     * return view;
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(fragLayoutRes(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        afterOnCreate();
    }

    public void onFrag2Act(String tag, @Nullable Object objectParam) {
        if (null != mListener) {
            mListener.onFrag2Act(tag, objectParam);
        }
    }

    @Override
    public void showToast(@NonNull final CharSequence sequence) {
        FragmentActivity activity = getActivity();
        if (null == activity || activity.isFinishing() || activity.isDestroyed())
            return;
        activity.runOnUiThread(() ->
                UIHelper.toastShort(activity, sequence));
    }

    @Override
    public void showErrorToast(@NonNull CharSequence sequence, Throwable e) {
        String error = ErrorHelper.getMessage(e);
        String toastStr = replaceText(sequence) + (TextUtils.isEmpty(error) ? "" : "\n" + error);
        showToast(toastStr);
    }

    @Override
    public void showDialog(@NonNull CharSequence sequence) {
        showDialog(sequence, false);
    }

    @Override
    public void showDialog(@NonNull CharSequence sequence, boolean finish) {
        Context context = getContext();
        if (null != context)
            showMessageDialog(replaceText(sequence), finish);
    }

    @Override
    public String replaceText(CharSequence sequence) {
        return ResUtil.replaceText(sequence);
    }

    @Override
    public void showErrorDialog(@NonNull CharSequence sequence, Throwable e, boolean finish) {
        String error = ErrorHelper.getMessage(e);
        String str = replaceText(sequence) + (TextUtils.isEmpty(error) ? "" : "\n" + error);
        showDialog(replaceText(str), finish);
    }

    @Override
    public void showErrorDialog(@NonNull CharSequence sequence, Throwable e) {
        showErrorDialog(sequence, e, false);
    }

    public @Nullable
    Intent getIntent() {
        FragmentActivity activity = getActivity();
        if (null != activity)
            return activity.getIntent();
        return null;
    }

    public void finish() {
        FragmentActivity activity = getActivity();
        if (null != activity)
            activity.finish();
    }

    public void showMessageDialog(CharSequence content, final boolean isFinish) {
        FragmentActivity activity = getActivity();
        if (null != activity) DialogHelper.showInActivity(activity, content, isFinish);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbind();
    }

    public void unbind() {
    }
}
